package com.compty.paint.domain;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.compty.core.framework.context.spring.SpringContextHolder;
import com.compty.paint.manager.CatalogTypeManager;
import com.compty.paint.util.Constants;
import com.compty.security.domain.User;

/**
 * @Todo:绘画作品
 * @author: zhuqinghui
 * @CreateTime:2011-5-31 下午06:47:13
 */
@Entity
@Table(name = "t_painting")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Painting implements java.io.Serializable{
	
	private static final long serialVersionUID = -1425682165499041273L;
	
	private Long id;
	
	/************ 作品名称 ****************/
	private String title;
	
	/************ 画家 ****************/
	private Painter painter;
	
	/************ 作品一级类别 ****************/
	private CatalogType catalogType;
	
	/************ 作品二级类别  ****************/
	private PaintType paintType;
	
	/************ 作品材质  ****************/
	private String caiZhi;
	
	/************ 作品形状  ****************/
	private SharpInfo sharpInfo;
	
	/************ 上传时间 ****************/
	private Date uploadTime;
	
	/************ 最后修改时间  ****************/
	private Date modifyTime;
	
	/************ 作品水平长度****************/
	private Float width;
	
	/************ 作品垂直长度  ****************/
	private Float hight;
	
	/*************作品海拔高度 **************/
	private Float altitude;
	
	/************ 标价  ****************/
	private Float price;
	private Float bidPrice;//起拍价
	
	/************ 上传状态  ****************/
	private UploadState uploadState;
	
	/************ 审核状态  ****************/
	private AuditState auditState;
	
	/************ 是否带框销售 ****************/
	private boolean hasBorder ;
	
	/************ 录入人  ****************/
	private User entryPerson;
	
	/************ 图片  ****************/
	private ImageInfo imageInfo;
	
	private String provider;//提供方
	
	private String onSaleFlag = "0";// 该值为1表示上拍销售,0表示不销售,2表示直接销售
	
	private ImageInfo zhengshu;//证书
	
	private Integer qqxl;//全球限量数目 (限量版时填写)
	private String unit;//单位
	private String unit2;//单位2
	private Integer store;//库存数量
	
	private String remark;//备注(1:真迹  2:高仿 等选项)
	
	private String originalImagePath;
	private String middleImageApath;
	private String middleImageBpath;
	private String smallImagePath;
	private String zhengShuImagePath;
	private String linkUrl;
	
	private ImageInfo imageInfo2;//第二张图片
	private ImageInfo imageInfo3;//第二张图片
	private ImageInfo imageInfo4;//第二张图片
	
	private String originalImagePath2;
	private String middleImageBpath2;
	private String smallImagePath2;
	
	private String originalImagePath3;
	private String middleImageBpath3;
	private String smallImagePath3;
	
	private String originalImagePath4;
	private String middleImageBpath4;
	private String smallImagePath4;
	
	private Integer sortFlag = 0;//是否排序标志 0:不排序 1：排序
	private Integer orderNo;//排序位置
	
	@Id
	@GeneratedValue
	@Column(name = "id", unique = true, nullable = false)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name = "title", length = 100)
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name="painterid")
	public Painter getPainter() {
		return painter;
	}

	public void setPainter(Painter painter) {
		this.painter = painter;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="painttype_id")
	public PaintType getPaintType() {
		return paintType;
	}

	public void setPaintType(PaintType paintType) {
		this.paintType = paintType;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="sharp_id")
	public SharpInfo getSharpInfo() {
		return sharpInfo;
	}

	public void setSharpInfo(SharpInfo sharpInfo) {
		this.sharpInfo = sharpInfo;
	}

	@Column(name = "uploadtime")
	public Date getUploadTime() {
		return uploadTime;
	}

	public void setUploadTime(Date uploadTime) {
		this.uploadTime = uploadTime;
	}

	@Column(name = "modifytime")
	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}

	@Column(name = "width")
	public Float getWidth() {
		return width;
	}

	public void setWidth(Float width) {
		this.width = width;
	}

	@Column(name = "hight")
	public Float getHight() {
		return hight;
	}

	public void setHight(Float hight) {
		this.hight = hight;
	}
	
	@Column(name = "altitude")
	public Float getAltitude() {
		return altitude;
	}

	public void setAltitude(Float altitude) {
		this.altitude = altitude;
	}

	@Column(name = "price")
	public Float getPrice() {
		return price;
	}

	public void setPrice(Float price) {
		this.price = price;
	}

	@Column(name = "bid_price")
	public Float getBidPrice() {
		return bidPrice;
	}

	public void setBidPrice(Float bidPrice) {
		this.bidPrice = bidPrice;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="uploadstate_id")
	public UploadState getUploadState() {
		return uploadState;
	}

	public void setUploadState(UploadState uploadState) {
		this.uploadState = uploadState;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="auditstate_id")
	public AuditState getAuditState() {
		return auditState;
	}

	public void setAuditState(AuditState auditState) {
		this.auditState = auditState;
	}

	@Column(name = "cai_zhi",length=40)
	public String getCaiZhi() {
		return caiZhi;
	}

	public void setCaiZhi(String caiZhi) {
		this.caiZhi = caiZhi;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="userinfo_id")
	public User getEntryPerson() {
		return entryPerson;
	}

	public void setEntryPerson(User entryPerson) {
		this.entryPerson = entryPerson;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="catalogtype_id")
	public CatalogType getCatalogType() {
		return catalogType;
	}

	public void setCatalogType(CatalogType catalogType) {
		this.catalogType = catalogType;
	}

	@Column(name = "is_border")
	public boolean isHasBorder() {
		return hasBorder;
	}

	public void setHasBorder(boolean hasBorder) {
		this.hasBorder = hasBorder;
	}

	@ManyToOne(fetch = FetchType.LAZY,cascade =  {CascadeType.REMOVE })
	@JoinColumn(name="image_id")
	public ImageInfo getImageInfo() {
		return imageInfo;
	}

	public void setImageInfo(ImageInfo imageInfo) {
		this.imageInfo = imageInfo;
	}

	@Column(name = "onsale_flag",length = 10)
	public String getOnSaleFlag() {
		return onSaleFlag;
	}

	public void setOnSaleFlag(String onSaleFlag) {
		this.onSaleFlag = onSaleFlag;
	}

	@Column(name = "provider",length = 50)
	public String getProvider() {
		return provider;
	}

	public void setProvider(String provider) {
		this.provider = provider;
	}

	@ManyToOne(fetch = FetchType.LAZY,cascade =  {CascadeType.REMOVE })
	@JoinColumn(name="zhengshu_imageid")
	public ImageInfo getZhengshu() {
		return zhengshu;
	}

	public void setZhengshu(ImageInfo zhengshu) {
		this.zhengshu = zhengshu;
	}

	@Column(name = "qqxl")
	public Integer getQqxl() {
		return qqxl;
	}

	public void setQqxl(Integer qqxl) {
		this.qqxl = qqxl;
	}

	@Column(name = "remark",length = 10)
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	@Column(name = "unit",length = 10)
	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	@Column(name = "unit2",length = 10)
	public String getUnit2() {
		return unit2;
	}

	public void setUnit2(String unit2) {
		this.unit2 = unit2;
	}

	@Column(name = "store")
	public Integer getStore() {
		return store;
	}

	public void setStore(Integer store) {
		this.store = store;
	}

	@Transient
	public String getOriginalImagePath() {
		if( imageInfo!= null ){
			originalImagePath = prefixUrlOfImage()+ imageInfo.getId() +"."+ imageInfo.getSuffix();
		}
		return originalImagePath;
	}

	@Transient
	public String getMiddleImageApath() {
		if( imageInfo!= null){
			middleImageApath =  prefixUrlOfImage() + imageInfo.getId() +"_m_a."+ imageInfo.getSuffix();
		}
		return middleImageApath;
	}

	@Transient
	public String getMiddleImageBpath() {
		if( imageInfo!= null){
			middleImageBpath =  prefixUrlOfImage() + imageInfo.getId() +"_m_b."+ imageInfo.getSuffix();
		}
		return middleImageBpath;
	}

	@Transient
	public String getSmallImagePath() {
		if( imageInfo!= null){
			smallImagePath = prefixUrlOfImage()+ imageInfo.getId() +"_s."+ imageInfo.getSuffix();
		}
		return smallImagePath;
	}
	
	@Transient
	public String getZhengShuImagePath() {
		if( zhengshu != null){
			zhengShuImagePath = Constants.IMAGE_SERVER +Constants.zhengshuFlag+"/"+ zhengshu.getId() +"."+ zhengshu.getSuffix();
		}
		return zhengShuImagePath;
	}

	private String prefixUrlOfImage(){
		StringBuilder sb = new StringBuilder(Constants.IMAGE_SERVER);
		if( catalogType != null){
			if( 1 == catalogType.getId() ||  2 == catalogType.getId()){
				if( painter != null){
					if( "2".equals(painter.getType())){//名家名画
						sb.append("painting/").append(painter.getId()).append("/");
					}else{
						sb.append("shuhua/").append(painter.getId()).append("/");
					}
				}else{
					sb.append("shuhua/");
				}
			}
			else if(  catalogType.getId() >= 3 ){ 
				CatalogTypeManager catalogTypeManager = SpringContextHolder.getBean("catalogTypeManager");
				catalogType = catalogTypeManager.get(catalogType.getId());
				String foldName = catalogType.getEnglish_name();
				if( painter != null){
					sb.append(foldName+"/").append(painter.getId()).append("/");
				}else{
					sb.append(foldName+"/");
				}
			}
		}else{
			if( painter != null){
				sb.append("painting/").append(painter.getId()).append("/");
			}else{
				sb.append("painting/");
			}
		}
		return sb.toString();
	}

	@ManyToOne(fetch = FetchType.LAZY,cascade =  {CascadeType.REMOVE })
	@JoinColumn(name="image_id2")
	public ImageInfo getImageInfo2() {
		return imageInfo2;
	}

	public void setImageInfo2(ImageInfo imageInfo2) {
		this.imageInfo2 = imageInfo2;
	}

	@ManyToOne(fetch = FetchType.LAZY,cascade =  {CascadeType.REMOVE })
	@JoinColumn(name="image_id3")
	public ImageInfo getImageInfo3() {
		return imageInfo3;
	}

	public void setImageInfo3(ImageInfo imageInfo3) {
		this.imageInfo3 = imageInfo3;
	}

	@ManyToOne(fetch = FetchType.LAZY,cascade =  {CascadeType.REMOVE })
	@JoinColumn(name="image_id4")
	public ImageInfo getImageInfo4() {
		return imageInfo4;
	}

	public void setImageInfo4(ImageInfo imageInfo4) {
		this.imageInfo4 = imageInfo4;
	}

	@Transient
	public String getLinkUrl() {
		if( painter != null && "2".equals(painter.getType())){//名家名画
				this.linkUrl = "mj/mjzp_"+id+".html";
		}else{
			judgeByCatalogtype();
		}
		return linkUrl;
	}
	
	private void judgeByCatalogtype(){
		String painterName = painter!= null && painter.getEnglishName()!= null ? painter.getEnglishName()+"/":"";
		if(catalogType != null){
			if( 1 == catalogType.getId() ||  2 == catalogType.getId()){//书画
				this.linkUrl = "shuhua/"+painterName+id+".html";
			}
			else if(  3 == catalogType.getId() ){ //瓷器
				this.linkUrl = "ciyi/"+painterName+id+".html";
			}
			else{
				this.linkUrl = "shuhua/"+painterName+id+".html";
			}
		}
		else{
			this.linkUrl = "shuhua/"+painterName+id+".html";
		}
	}

	@Transient
	public String getOriginalImagePath2() {
		if( imageInfo2!= null ){
			originalImagePath2 = prefixUrlOfImage()+ imageInfo2.getId() +"."+ imageInfo2.getSuffix();
		}
		return originalImagePath2;
	}
	
	@Transient
	public String getOriginalImagePath3() {
		if( imageInfo3!= null ){
			originalImagePath3 = prefixUrlOfImage()+ imageInfo3.getId() +"."+ imageInfo3.getSuffix();
		}
		return originalImagePath3;
	}
	
	@Transient
	public String getOriginalImagePath4() {
		if( imageInfo4!= null ){
			originalImagePath4 = prefixUrlOfImage()+ imageInfo4.getId() +"."+ imageInfo4.getSuffix();
		}
		return originalImagePath4;
	}

	@Transient
	public String getMiddleImageBpath2() {
		if( imageInfo2 != null){
			middleImageBpath2 =  prefixUrlOfImage() + imageInfo2.getId() +"_m_b."+ imageInfo2.getSuffix();
		}
		return middleImageBpath2;
	}
	
	@Transient
	public String getMiddleImageBpath3() {
		if( imageInfo3 != null){
			middleImageBpath3 =  prefixUrlOfImage() + imageInfo3.getId() +"_m_b."+ imageInfo3.getSuffix();
		}
		return middleImageBpath3;
	}

	@Transient
	public String getMiddleImageBpath4() {
		if( imageInfo4!= null){
			middleImageBpath4 =  prefixUrlOfImage() + imageInfo4.getId() +"_m_b."+ imageInfo4.getSuffix();
		}
		return middleImageBpath4;
	}

	@Transient
	public String getSmallImagePath2() {
		if( imageInfo2 != null){
			smallImagePath2 = prefixUrlOfImage()+ imageInfo2.getId() +"_s."+ imageInfo2.getSuffix();
		}
		return smallImagePath2;
	}

	@Transient
	public String getSmallImagePath3() {
		if( imageInfo3 != null){
			smallImagePath3 = prefixUrlOfImage()+ imageInfo3.getId() +"_s."+ imageInfo3.getSuffix();
		}
		return smallImagePath3;
	}

	@Transient
	public String getSmallImagePath4() {
		if( imageInfo4 != null){
			smallImagePath4 = prefixUrlOfImage()+ imageInfo4.getId() +"_s."+ imageInfo4.getSuffix();
		}
		return smallImagePath4;
	}
	
	@Column(name="sortflag")
	public Integer getSortFlag() {
		return sortFlag;
	}

	public void setSortFlag(Integer sortFlag) {
		this.sortFlag = sortFlag;
	}

	@Column(name="order_no")
	public Integer getOrderNo() {
		return orderNo;
	}

	public void setOrderNo(Integer orderNo) {
		this.orderNo = orderNo;
	}
}
